package com.lijie.userservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lijie.userservice.entity.TbUser;
import com.lijie.userservice.listen.DataEasyExcelListstener;
import com.lijie.userservice.mapper.TbUserMapper;
import com.lijie.userservice.service.TbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lijie.userservice.vo.TbUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ange
 * @since 2020-07-19
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {
    @Override
    public List<TbUser> findUserAndDept(Integer current,Integer size) {
        return  this.baseMapper.findUserAndDept(current,size);
    }

    @Override
    public Integer findUserAndDeptCount() {
        return this.baseMapper.findUserAndDeptCount();
    }

    @Override
    public IPage<TbUser> findUserPage(Page<TbUser> page, QueryWrapper<TbUser> wrapper) {
        return this.baseMapper.findUserPage(page,wrapper);
    }


    @Override
    public void exportUserInfoExcel(HttpServletResponse response,List<TbUser> tbUsers) throws IOException {
        //获取所有数据


        //写入excel文件
        String exportFileName="TbUser.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition","attachment;filename="+exportFileName);
        EasyExcel.write(response.getOutputStream(),TbUser.class)
                .sheet("用户信息")
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(tbUsers);
    }

    /**
     * 导入信息
     *
     * @param file
     * @throws IOException
     */
    @Override
    public void importUserInfoExcel(MultipartFile file) throws IOException {
        //Excel文件校验


        //读取数据
        DataEasyExcelListstener<TbUser> liststener=new DataEasyExcelListstener<>();
        InputStream inputStream=file.getInputStream();
        EasyExcel.read(inputStream,TbUser.class,liststener)
                .sheet(0)
                .doRead();
        List<TbUser> data=liststener.getData();
        System.out.println("导入的用户信息是：");
        System.out.println(data);


        //数据存储
        data.forEach(student -> {
            this.baseMapper.insert(student);
        });

    }
    @Override
    public boolean updStatus(Integer id, Integer status) {
        return baseMapper.updateStatus (id,status);
    }


}
